/**
* 修改日期：2020-12-20
*/
import dayjs from 'dayjs'
/**1.日期格式化
* 示例：dateformat(new Date())、dateformat('2018-06-03')
*/
export const dateformat = (dataStr, pattern = 'YYYY-MM-DD') => {
  return dayjs(dataStr).format(pattern)
}
/**2. 格式化金额
* 示例：formatCurrency(99.312) =>99.31
*/
export const formatCurrency = (val, floatType = 2) => {
  val = (val + '').replace(/\$|\,/g, '')
  if (isNaN(val)) {
    val = '0'
  }
  let sign = (val == (val = Math.abs(val)))
  val = Math.floor(val * 100 + 0.50000000001)
  let cents = val % 100
  val = Math.floor(val / 100).toString()
  if (cents < 10) {
    cents = '0' + cents
  }
  for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
    val = val.substring(0, val.length - (4 * i + 3)) + ',' + val.substring(val.length - (4 * i + 3))
  }
  if (floatType == 2) val += '.' + cents
  return (((sign) ? '' : '-') + val)
}

/**3. 截取url参数
* 示例：GetRequestParams().userid
*/
export const GetRequestParams = () => {
  let url = location.search // 获取url中"?"符后的字串
  let theRequest = new Object()
  if (url.indexOf('?') != -1) {
    let str = url.substr(1)
    let strs = str.split('&')
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
    }
  }
  return theRequest
}
//4. 万元转换
export const formatAmountUnit = (num) => {
  let money = Number(num)
  if (money > 9999) {
    return money / 10000 + '万'
  }
  return money
}
//5. 转换json字符串
export const toJson = (obj) => {
  return typeof (obj) === 'string' ? eval('(' + obj + ')') : obj
}
//6. uuid
export const guid = () =>{
	function S4() {
		return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	}
	return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}